package com.example.demo.threadPool_countDownLatch.plus;

import java.util.concurrent.*;

/**
 * 线程池工具类
 */
public class ThreadPoolUtils {

    private volatile static ThreadPoolExecutor threadPool;
    public static final int CORE_POOL_SIZE = 30;
    public static final int MAX_POOL_SIZE = 15;
    public static final int KEEP_ALIVE_TIME = 180;
    public static final int BLOCK_QUEUE_SIZE = 30;

    public static void executor(Runnable runnable) {
        getThreadPoolExecutor().execute(runnable);
    }

    public static <T> Future<T> submit(Callable<T> callable) {
        return getThreadPoolExecutor().submit(callable);
    }

    /**
     * 获取线程池对象
     *
     * @return
     */
    public static ThreadPoolExecutor getThreadPoolExecutor() {
        if (threadPool == null) {
            synchronized (ThreadPoolUtils.class) {
                if (threadPool == null) {
                    threadPool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.SECONDS,
                            new LinkedBlockingQueue<>(BLOCK_QUEUE_SIZE), new ThreadPoolExecutor.CallerRunsPolicy());
                }
            }
        }
        return threadPool;
    }
}